require(["../../conf/config.js"], function(){
	
	require(["h&f", "template" ,"jquery", "jquery.cookie"],function(hf,template){
		
		/*---------------------------- 全局变量区 ------------------------*/
		
		
		
		/*---------------------------- 引用部分 ------------------------*/
		
		// 模块添加上面浮动导航栏和下面固定部分
		addTopAndBottomFn();
		//获取缓存中的商品并且放入表格中
		let shopCarList = getShopCarList();
		
		setCookieToTable(shopCarList);
		//开始数据控制
		beginDataCtrl(shopCarList);
		
		/*---------------------------- 实现部分 ------------------------*/	
		
			// 模块添加上面浮动导航栏和下面固定部分实现
			function addTopAndBottomFn(){
				
				document.body.innerHTML = hf.headerHTML + document.body.innerHTML + hf.footerShopHTML + hf.footerHTML;
				
				hf.headerJS();
				
				hf.footerShopJS();
			}
			
			//获取缓存中的数组实现
			function getShopCarList(){
				
				return JSON.parse($.cookie("goodsShop"));
			}
			//根据商品输出表格实现
			function setCookieToTable(list){
				
				let htmlStr = template("shopCarList",{
					
					list : list
				});
				
				$("tbody").html(htmlStr);
			}
	
			//开始数据控制实现
			function beginDataCtrl(dataList){

				/*--------  选择部分 --------*/
				//所有数据
				let sourceArr = null;
				//选中的数据
				let selectedArr = [];
				//全选按钮
				let $allBtn = null;
				//所有item选择按钮
				let $itemBtnArr = null;
				
				//减按钮组
				let $reduceBtnArr = null;
				//加按钮组
				let $pulsBtnArr = null
				//数量显示组
				let $numLabelArr = null;
				//第一价格组
				let $singleNewPriceArr = null;
				//第一价格总数组
				let $totelSingleNewPriceArr = null;
				//第二价格组
				let $singleOldPriceArr = null;
				//第二价格总数组
				let $totelSingleOldPriceArr = null;
				//总金额
				let $allPay = null;
				//原总金额
				let $oldAllPay = null;
				//总节省
				let $allSave = null;
				
				//删除按钮组
				let $deleteBtnArr = null; 
				 
				initData(dataList); 
				
				addAllItemToSelected();
				
				//设置按钮选状态
				setSelectBtnsStatus(); 
				
				//初始化
				 function initData(list){
					 
					 //所有数据
					 sourceArr = list;
					 //全选按钮
					 $allBtn = $("thead td.col1 input");
					 //所有item选择按钮
					 $itemBtnArr = $("tbody td.col1 input");
					 
					 initCompute();
					 //删除按钮
					 $deleteBtnArr = $("tbody a.deleteBtn");
					 //添加选择按钮点击
					 setAllBtnClick();
					 
					 //添加减少增加按钮点击
					 setReducePlusBtnsClick();
					 
					 //添加删除按钮点击
					 setDeleteBtnsClick();
				 }
				//全选所有按钮
				function addAllItemToSelected(){
					
					sourceArr.forEach(function(item){
						
						selectedArr.push(item.id);
						
					});
					
				}
				//添加选择按钮点击实现
				function setAllBtnClick(){
					
					$allBtn.on("click",function(){
						
						selectedArr = [];
						
						if($(this).prop("checked")){
						
							addAllItemToSelected();
							
						}
						setSelectBtnsStatus();
					});
					
					$itemBtnArr.on("click",function(){
						
						let idx = $itemBtnArr.index(this);
						
						let item = sourceArr[idx];
						
						let selectIdx = selectedArr.indexOf(item.id);
						
						if(selectIdx == -1){
							
							selectedArr.push(item.id);
							
						}else{
							
							selectedArr.splice(selectIdx, 1);
						}
						
						setSelectBtnsStatus();
						
					});
				}
				
				
				//item的选中设置
				function setSelectBtnsStatus(){
					 
					 let ifAll = sourceArr.length > 0 ? true : false;
					 
					 sourceArr.forEach(function(item, index){
						 
						 let ifHave = selectedArr.indexOf(item.id) == -1 ? false : true;
						 
						 $itemBtnArr.eq(index).prop("checked", ifHave);
						 
						 if(!ifHave){
							 
							 ifAll = false;
							 
						 }
						 
					 });
					 
					 $allBtn.prop("checked", ifAll);
					 
					 computeTotalCash();
				 }
				
				/*--------  计算部分 --------*/
				function initCompute(){
					//减按钮组
					$reduceBtnArr = $("tbody .col3 a.reduce");
					//加按钮组
					$pulsBtnArr = $("tbody .col3 a.puls");
					//数量显示组
					$numLabelArr = $("tbody .col3 span.currentNum");
					//第一价格组
					$singleNewPriceArr = $(".singleNewPrice");
					//第一价格总数组
					$totelSingleNewPriceArr = $(".totelSingleNewPrice");
					//第二价格组
					$singleOldPriceArr = $(".singleOldPrice");
					//第二价格总数组
					$totelSingleOldPriceArr = $(".totelSingleOldPrice");
					//总金额
					$allPay = $("#showTotalPrice");
					//原总金额
					$oldAllPay = $("#oldTotalPrice");
					//总节省
					$allSave = $("#saveMoney");
				}
				/*--------  计算数量部分 --------*/
				//添加减少增加按钮点击
				function setReducePlusBtnsClick(){
					
					$reduceBtnArr.on("click",function(){
						
						let idx = $reduceBtnArr.index(this);
						
						let item = sourceArr[idx];
						
						let newNum = changeNumForGoodsIdForCookie(item.id, -1);
						
						if (newNum != -1){
							
							item.num = newNum;
							
							$numLabelArr.eq(idx).text(item.num);
							
							computeItemCash(idx, item);
						}
					});
					
					$pulsBtnArr.on("click",function(){
						
						let idx = $pulsBtnArr.index(this);
						
						let item = sourceArr[idx];
						// console.log(idx);
						let newNum = changeNumForGoodsIdForCookie(item.id, +1);
						
						if (newNum != -1){
							
							item.num = newNum;
							
							$numLabelArr.eq(idx).text(item.num);
							
							computeItemCash(idx, item);
						}
					});
				}
				
				//根据cookie计算数据增减
				function changeNumForGoodsIdForCookie(goods_id, num){
					// console.log(435435434);
					let cookieList = getShopCarList();
					
					let theGoods = cookieList.find(function(goods){
						
						return goods.id == goods_id;
						
					});
					
					if(!theGoods) return -1;
					
					let newNum = theGoods.num + num;
					
					if(newNum < 1){
						
						return -1;
					}
					
					theGoods.num = newNum;
					
					try{
						
						$.cookie("goodsShop", JSON.stringify(cookieList), { expires: 7 ,path: '/'});
						
					}catch(e){
						//TODO handle the exception
						console.log(e);
						
						return -1
					}
					// console.log($.cookie("goodsShop"));
					
					return newNum;
				}
				
				/*--------  计算金额部分 --------*/
				//计算当前item金额
				function computeItemCash(index, item){
					
					let nowTotal = item.neoPrice == 0 ? item.num * item.price : item.num * item.neoPrice;
					// console.log(item);
					let oldTotal = item.price * item.num;
					
					$totelSingleNewPriceArr.eq(index).text(nowTotal.toFixed(2));
					
					$totelSingleOldPriceArr.eq(index).text(oldTotal.toFixed(2));
					
					if(selectedArr.indexOf(item.id) != -1){
						
						computeTotalCash();
					}
				}
				//计算总金额
				function computeTotalCash(){
					
					let totalNowCash = 0;
					
					let totalOldCash = 0;
					
					let totalSaveCash = 0;
					
					sourceArr.forEach(function(item, index){ 
						
						if(selectedArr.indexOf(item.id) != -1){
							
							computeItemTotalCash(item, index)
						}
						
					});
					
					$allPay.text("¥"+totalNowCash.toFixed(2));
					
					$oldAllPay.text(totalOldCash.toFixed(2));
					
					$allSave.text(totalSaveCash.toFixed(2));
					//列入计算
					function computeItemTotalCash(item, index){
						
						let currentPrice = item.neoPrice ? item.neoPrice : item.price;
						
					 	totalNowCash += currentPrice * item.num;
						
						totalOldCash += item.price * item.num;
						
						totalSaveCash = totalOldCash - totalNowCash;
					}
					
				}
				
				
				/*--------  删除 --------*/
				
				
				//删除当前item并操作cookie以及刷新页面
				
				function setDeleteBtnsClick(){
					
					$deleteBtnArr.on("click", function(){
						
						deleteItem($deleteBtnArr.index(this));
					});
					
				}
				
				//删除当前item，重新获取所有数据
				function deleteItem(index){
					
					let item = sourceArr[index];
					
					let cookieList = getShopCarList();
					
					let cooIdx = cookieList.findIndex(function(goods){
						
						return goods.id == item.id;
						
					});
					
					if(cooIdx == -1) return;
					
					cookieList.splice(cooIdx, 1);
					
					try{
						
						$.cookie("goodsShop", JSON.stringify(cookieList), { expires: 7 ,path: '/'});
						
					}catch(e){
						//TODO handle the exception
						console.log(e);
						
						return ;
					}
					
					$("tbody").empty();
					
					let shopCarList = getShopCarList();
					
					setCookieToTable(shopCarList);
					
					initData(shopCarList);
					
					setSelectBtnsStatus(); 
				}
				
				
			}
	})
});